package main

import (
	"bufio"
	"fmt"
	"net/url"
	"os"
	"regexp"
	"strings"
)

func addBanner(line string) string {

	re := regexp.MustCompile("\\[[^\\]]*\\]\\(([^\\)]*)\\)")
	matches := re.FindStringSubmatch(line)
	if len(matches) > 1 {

		u, e := url.Parse(matches[1])
		if e != nil {
			return line
		}

		vs := strings.Split(u.Path, "/")
		if len(vs) == 3 {

			rep := matches[0] + " ![](https://img.shields.io/github/stars/" + vs[1] + "/" + vs[2] + "?style=social)"
			rep = re.ReplaceAllString(line, rep)
			return rep
		} else {
			return line
		}

	} else { // 标题
		return line
	}
}

func main() {
	print("hello")

	f, e := os.Open("README.md")
	defer f.Close()

	if e != nil {
		fmt.Println("error open file")
	}

	buf := bufio.NewScanner(f)

	for {
		if !buf.Scan() {
			break
		}

		line := buf.Text()

		trimmed := strings.TrimSpace(line)
		if strings.HasPrefix(trimmed, "*") {
			line = addBanner(line)
		}
		fmt.Printf("%v\n", line)

	}

}
